package com.sense.kukoo.common.wiget.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;

public class DeleteItemPopupWindow extends PopupWindow {

    private static final String TAG = DeleteItemPopupWindow.class.getSimpleName();
    private onDeleteListener mListener;

    public DeleteItemPopupWindow(View contentView, int width, int height, boolean focusable) {
        super(contentView, width, height, focusable);
    }

    public static DeleteItemPopupWindow create(Context context, ViewGroup root){
        View inflate = LayoutInflater.from(context).inflate(R.layout.common_popup_delete_item, root, false);
        int width = ApplicationContext.dp2px(200);
        int height = ApplicationContext.dp2px(44);

        DeleteItemPopupWindow popupWindow = new DeleteItemPopupWindow(inflate, width, height, true);
        //
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setElevation(4);
        //
        View item = popupWindow.getContentView().findViewById(R.id.item);
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.mListener!=null) {
                    popupWindow.mListener.onDelete();
                }
                popupWindow.dismiss();
            }
        });

        return popupWindow;
    }

    public interface onDeleteListener{
        void onDelete();
    }

    public DeleteItemPopupWindow setListener(onDeleteListener l){
        mListener = l;
        return this;
    }

    public DeleteItemPopupWindow show(View anchor, Boolean isUp){
        int xoff = - ApplicationContext.dp2px(16);
        int yoff = 0;
        if (isUp) {
            yoff -= getHeight() + anchor.getHeight();
        }
        showAsDropDown(anchor, xoff, yoff, Gravity.BOTTOM | Gravity.END);
        return this;
    }
}
